const app = getApp();
import { getStorage } from "../../../utils/localStorage"
import { personalInformation, otherDynamics_list, otherList_list, followUser_opt } from "../../../api/my"
import { baseUrl } from "../../../api/request"
Page({
    data: {
        navData: {
            level: 2,
            title: "个人主页"
        },
        baseUrl: baseUrl,
        tabIndex: 0,
        user: getStorage('user'),
        datetilObj: {},
        personData: {},
        praiseList: [],
        navMargin: app.globalData.navHeight + app.globalData.navTop,
    },
    onLoad(options) {
        let item = JSON.parse(decodeURIComponent(options.item));
        this.setData({
            baseUrl: baseUrl,
            user: getStorage('user'),
            datetilObj: item,
        });
        this.getGenRen();
    },
    onReady() {

    },
    onShow() {

    },
    //获取个人信息
    getGenRen() {
        personalInformation({ id: this.data.user.id, userId: this.data.datetilObj.id }).then(res => {
            this.setData({
                personData: res.data.data
            });
            this.getOtherDynamics_list();
        })
    },
    //获取个人的乡音数据
    getOtherDynamics_list() {
        otherDynamics_list({ userId: this.data.personData.id }).then(res => {
            if (res.data.rows.lenght != 0) {
                res.data.rows.forEach(item => {
                    item.imageScale = item.imageScale.split(",");
                });
                this.setData({
                    praiseList: res.data.rows,
                });
            } else {
                this.setData({
                    praiseList: [],
                });
            }
        });
    },
    //获取个人的赞过数据
    getOtherLike_list() {
        otherList_list({ userId: this.data.personData.id }).then(res => {
            if (res.data.rows.lenght != 0) {
                res.data.rows.forEach(item => {
                    item.imageScale = item.imageScale.split(",");
                });
                this.setData({
                    praiseList: res.data.rows,
                });
            } else {
                this.setData({
                    praiseList: [],
                });
            }

        });
    },
    tabClick(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            praiseList: [],
            tabIndex: index
        });
        if (index === 0) {
            this.getOtherDynamics_list();
        } else {
            this.getOtherLike_list();
        };

    },
    //关注按钮事件
    followOpt(e) {
        let item = e.currentTarget.dataset.item,
            state = item.state ? false : true;
        if (state) {
            this.getFollow(item.id, state);

        } else {
            wx.showModal({
                title: '用户关注',
                content: '请确定是否要取消对该用户的关注',
                complete: (res) => {
                    if (res.confirm) {
                        this.getFollow(item.id, state);
                    };
                }
            });
        };

    },
    //关注操作请求
    getFollow(id, state) {
        followUser_opt({ id: this.data.user.id, userId: id, state: state }).then(res => {
            if (res.data.code === 200) {
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 1000,
                })
                this.setData({
                    "personData.state": state
                });
            };
        });
    },
    //列表点击事件
    listClick(e) {
        let item = e.currentTarget.dataset.item;
        if (item.mark = "动态") {
            wx.navigateTo({
                url: '/pagesShouYe/pages/listDetail/index?id=' + item.id,
            })
        } else {
            wx.navigateTo({
                url: '/pagesShouYe/pages/bnnerDetailMore/index?id=' + item.id,
            })
        };
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})